﻿using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Anheledir.XmlDialogEditor {
  public partial class wizardKeywords : Form {
    string f = string.Empty;

    public wizardKeywords() {
      InitializeComponent();
    }

    public wizardKeywords( string s ) {
      InitializeComponent();
      f = s;
    }

    public string getText() {
      return textBox1.Text;
    }

    private void linkLabel1_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e ) {
      System.Diagnostics.Process.Start( "http://www.regular-expressions.info/quickstart.html" );
    }

    private void button1_Click_1( object sender, EventArgs e ) {
      if ( textBox1.Text != string.Empty ) {
        textBox1.Text += ",";
      }
      if ( checkBox1.Checked ) {
        textBox1.Text += "^(?!" + keyword.Text.ToLower() + ").*$";
      } else {
        textBox1.Text += keyword.Text.ToLower();
      }
      keyword.Text = "";
      checkBox1.Checked = false;
    }

    private void wizardKeywords_Load( object sender, EventArgs e ) {
      this.Text = languages.KeywordsAssistent;
      groupBox1.Text = languages.NewKeyword;
      groupBox2.Text = languages.TestKeywords;
      labelItemType.Text = languages.KeywordMayBeRegex;
      checkBox1.Text = languages.NOT;
      button1.Text = languages.Add;
      linkLabel1.Text = languages.BasicRegExTutorial;
      button2.Text = languages.Test;
      okButton.Text = languages.OK;
      cancelButton.Text = languages.Cancel;
      
      if ( f != string.Empty ) {
        textBox1.Text = f;
      }
      textBox1_TextChanged( sender, e );
    }

    private void textBox1_TextChanged( object sender, EventArgs e ) {
      if ( textBox1.Text != string.Empty ) {
        groupBox2.Enabled = true;
      } else {
        groupBox2.Enabled = false;
      }
    }

    private void button2_Click( object sender, EventArgs e ) {
      // The following code is based on ArteGordon's XMLSpawner to get a identical test-result as in Ultima Online
      string[] arglist = textBox1.Text.Split( ",".ToCharArray() );
      bool passed = false;
      bool error = false;
      string status_str = null;
      for ( int i = 0; i < arglist.Length; i++ ) {
        if ( arglist[i] == "*" ) {
          passed = true;
          break;
        } else {
          Regex r = null;
          try {
            r = new Regex( arglist[i], RegexOptions.IgnoreCase );
          } catch ( Exception ex ) { error = true; status_str = ex.Message; }
          if ( !error && r != null ) {
            Match m = r.Match( testKeyword.Text );
            if ( m.Success ) {
              passed = true;
              break;
            }
          }
        }
      }

      if ( error ) {
        testResult.Text = String.Format( languages.BadRegularExpression, status_str );
        testResult.ForeColor = Color.Red;
      } else if ( passed ) {
        testResult.Text = languages.Passed;
        testResult.ForeColor = Color.Green;
      } else {
        testResult.Text = languages.Failed;
        testResult.ForeColor = Color.Red;
      }
      timer1.Stop();
      timer1.Enabled = true;
      timer1.Start();
    }

    private void timer1_Tick( object sender, EventArgs e ) {
      testResult.Text = "";
      timer1.Stop();
      timer1.Enabled = false;
    }
  }
}
